import { selectTopicManageSetting, updateTopicManageSetting } from '@/services/api/api-cues-topic';
import { SettingOutlined } from '@ant-design/icons';
import { ProFormSwitch } from '@ant-design/pro-components';
import { DrawerForm, ProFormInstance } from '@ant-design/pro-form';
import { useAccess } from 'umi';
import { Button, message } from 'antd';
import React, { useRef, useState } from 'react';

interface TableTopicManageSettingProps {
  onRefurbish: () => void;
}

const TableTopicManageSetting: React.FC<TableTopicManageSettingProps> = ({ onRefurbish }) => {
  const access = useAccess();

  const formRef = useRef<ProFormInstance>();

  const [loading, setLoading] = useState(false);

  /**
   * 选题计划配置
   */
  const [settings, setSettingsDataState] = useState<CuesTopicData.TopicManageSetting>();

  /**
   * 加载 选题计划详情
   * @param key
   */
  const loadCuesTopicManageSetting = async () => {
    setLoading(true);
    try {
      const response = await selectTopicManageSetting();
      const { code, data } = response;
      if (code === 0) {
        setSettingsDataState(data);

        formRef.current?.setFieldsValue({
          manage: data.manage === 1,
        });

        setLoading(false);
      }
    } catch (error) {}
  };

  return (
    <DrawerForm
      key="table_topic_manage_setting_drawer_form"
      title="选题审批设置"
      resize={{
        onResize() {
          console.log('resize!');
        },
        maxWidth: window.innerWidth * 0.8,
        minWidth: 300,
      }}
      drawerProps={{
        // maskClosable: false,
        destroyOnClose: true,
        width: '50%',
      }}
      formRef={formRef}
      trigger={
        <Button type="primary" icon={<SettingOutlined />}>
          设置
        </Button>
      }
      initialValues={{}}
      onVisibleChange={(open) => {
        if (open) {
          loadCuesTopicManageSetting();
        }
      }}
      onValuesChange={async (values) => {
        setLoading(true);
        if (values.manage !== undefined) {
          const response = await updateTopicManageSetting({ manage: values.manage ? 1 : 0 });
          const { code, data } = response;
          if (code === 0) {
            message.success('修改成功');
            onRefurbish();
          }
        }
        setLoading(false);
      }}
      submitter={false}
    >
      <ProFormSwitch name="manage" label="审批" />
    </DrawerForm>
  );
};
export default TableTopicManageSetting;
